package com.lipuwater.agent.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.lipuwater.agent.R;
import com.lipuwater.agent.http.bean.product.ProductLeaseBean;
import com.lipuwater.agent.http.bean.product.ProductRulesBean;
import com.lipuwater.agent.ui.activity.ProduceOrderActivity;
import com.lipuwater.agent.ui.widget.TagsLayout;

import java.util.List;

import mehdi.sakout.fancybuttons.FancyButton;


/**
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃永无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━
 * Created by  Administrator  on 2016/12/23.
 * Email:924686754@qq.com
 * 产品规格
 */
public class ProductNormsDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private Callback mCallback;
    private List<ProductRulesBean> mProductRules = null;
    private RelativeLayout rootLayout;
    private TagsLayout tabsNorms;
    private TagsLayout tabsLease;
    private FancyButton btnOpen;
    private String normsid = "";//规格
    private String leaseid = ""; //租期id
    private String deposit = ""; //价格

    public ProductNormsDialog(Context context, List<ProductRulesBean> productRules, Callback callback) {
        super(context, R.style.MyDialogStyle);
        setContentView(R.layout.dialog_product_norms);
        this.mContext = context;
        this.mCallback = callback;
        this.mProductRules = productRules;
        initDialog();
        //只有一个的话默认选中跳转
        if (mProductRules != null && mProductRules.size() == 1 && mProductRules.get(0) != null) {
            normsid = mProductRules.get(0).getId();
            String normsidName = productRules.get(0).getName();
            if (mProductRules.get(0).getProductLease() != null && mProductRules.get(0).getProductLease().size() == 1 && mProductRules.get(0).getProductLease().get(0) != null) {
                leaseid = String.valueOf(Math.round(mProductRules.get(0).getProductLease().get(0).getId()));
                deposit = mProductRules.get(0).getProductLease().get(0).getDeposit();
                if (mCallback != null) {
                    mCallback.resultsNorms(normsid, normsidName);
                    mCallback.resultsLease(leaseid, deposit, true);
                }
            }
        }

    }


    private void initDialog() {
        rootLayout = (RelativeLayout) findViewById(R.id.root_layout);
        tabsNorms = (TagsLayout) findViewById(R.id.tabs_norms);
        tabsLease = (TagsLayout) findViewById(R.id.tabs_lease);
        btnOpen = (FancyButton) findViewById(R.id.btn_open);
        btnOpen.setOnClickListener(this);
        addNormsTas(tabsNorms, mProductRules);
        if (mProductRules != null && mProductRules.size() > 0) {
            addLeaseTas(tabsLease, mProductRules.get(0).getProductLease());
        }
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.BOTTOM);
        dialogWindow.setWindowAnimations(R.style.dialogStyle);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        lp.width = mContext.getResources().getDisplayMetrics().widthPixels;
        rootLayout.measure(0, 0);
        lp.height = rootLayout.getMeasuredHeight();
        lp.alpha = 9f;
        dialogWindow.setAttributes(lp);
        setCanceledOnTouchOutside(true);
    }

    private int oldIndexNorms = 0;

    private void addNormsTas(TagsLayout layoutTab, final List<ProductRulesBean> productRules) {
        int widthAndheight = ViewGroup.LayoutParams.WRAP_CONTENT;
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(widthAndheight, widthAndheight);
        for (int i = 0; i < productRules.size(); i++) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_product_tab, null);
            RadioButton radioButton = (RadioButton) view.findViewById(R.id.tab);
            radioButton.setText(productRules.get(i).getName());
            final int index = i;
            radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mCallback != null) {
                        mCallback.resultsNorms(productRules.get(index).getId(), productRules.get(index).getName());
                    }
                    normsid = productRules.get(index).getId();

                    if (index != oldIndexNorms) {
                        tabsLease.removeAllViews();
                        addLeaseTas(tabsLease, mProductRules.get(index).getProductLease());
                    } else if (tabsLease.getChildCount() == 0) {
                        addLeaseTas(tabsLease, mProductRules.get(index).getProductLease());
                    }
                    selectReplaceNorms(oldIndexNorms, index);
                }
            });
            layoutTab.addView(view, lp);
        }
    }

    private void selectReplaceNorms(int oldIndex, int index) {
        RadioButton oldRadioButton = (RadioButton) tabsNorms.getChildAt(oldIndex);
        RadioButton radioButton = (RadioButton) tabsNorms.getChildAt(index);
        oldRadioButton.setChecked(false);
        radioButton.setChecked(true);
        this.oldIndexNorms = index;
    }

    /**
     * 租期 -------------------------------------------------------------
     */
    private int oldIndexLease = 0;

    private void addLeaseTas(TagsLayout layoutTab, final List<ProductLeaseBean> productLease) {
        int widthAndheight = ViewGroup.LayoutParams.WRAP_CONTENT;
        ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(widthAndheight, widthAndheight);
        for (int i = 0; i < productLease.size(); i++) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_product_tab, null);
            RadioButton radioButton = (RadioButton) view.findViewById(R.id.tab);
            radioButton.setText(productLease.get(i).getName());
            final int index = i;
            radioButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    leaseid = String.valueOf(Math.round(productLease.get(index).getId()));
                    deposit = productLease.get(index).getDeposit();
                    if (mCallback != null) {
                        mCallback.resultsLease(leaseid, deposit, false);
                    }
                    selectReplaceLease(oldIndexLease, index);
                }
            });
            layoutTab.addView(view, lp);
        }
    }

    private void selectReplaceLease(int oldIndex, int index) {
        RadioButton oldRadioButton = (RadioButton) tabsLease.getChildAt(oldIndex);
        RadioButton radioButton = (RadioButton) tabsLease.getChildAt(index);
        oldRadioButton.setChecked(false);
        radioButton.setChecked(true);
        this.oldIndexLease = index;
    }

    @Override
    public void onClick(View v) {
        if (normsid.equals("") || leaseid.equals("")) {
            Toast.makeText(mContext, "请选择产品规格", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent(mContext, ProduceOrderActivity.class);
        intent.putExtra("normsid", normsid);
        intent.putExtra("leaseid", leaseid);
        intent.putExtra("deposit", deposit);
        mContext.startActivity(intent);
    }

    public void unCallback() {
        mCallback = null;
        mContext = null;
    }


    public interface Callback {
        void resultsNorms(String normsid, String normsidName);

        void resultsLease(String leaseid, String deposit, boolean jump);
    }
}